<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Model\Product;
use Illuminate\Support\Facades\Input;


class TestController extends Controller
{
    /**
     * //定义一个数组  用来排列查询选项或列表头
     * @var array
     */
    protected static $field = [
        'id'=>'编号',
        'rank'=>'权重',
        'update_time'=>'更新时间',
        'name'=>'产品名称',
        'price'=>'产品估价',
        'period'=>'制作周期',
        'firm'=>'适用企业',
        'performance'=>'绩效系数',
        'status'=>'状态'
    ];
    protected static $field2 = ['产品图片','产品附件','操作'];
    /**
     * 定义一个数组  用来定义表title等内容
     * @var array
     */
    protected static $table = [
        'name'=>'表明',
        'title'=>'标题',
        'subtitle'=>'小标题'
    ];

    /**
     * 测试列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function dataLists()
    {
        #表格、字段信息
        $field = self::$field;
        $field2 = self::$field2;
        $table = self::$table;

        #实例化产品对象
        $obj = new Product();
        #检测是否有搜索条件
        $search = Input::all();
        if($search){
            if(!empty($search['field'])){
                if(!empty($search['keyword'])){
                    $keyword = $search['keyword'];
                    $obj = $obj->where($search['field'],'like',"%$keyword%");
                }
                if(!empty($search['start_time'])){
                    $start_time = $search['start_time'];
                    $obj = $obj->where('update_time','>=',$start_time);
                }
                if(!empty($search['end_time'])){
                    $end_time = $search['end_time'];
                    $obj = $obj->where('update_time','<=',$end_time);
                }
            }
        }
        $info = $obj->paginate(10);
        $button = array();
        foreach ($info as $k=>$v){
            #按钮
            if($v['status'] == 0){
                $btn_status = $this->getStatusBtn($v['status'],$v['id'],'success','启用');
            }else{
                $btn_status = $this->getStatusBtn($v['status'],$v['id'],'warning','禁用');
            }
            #管理图片按钮
            $pictureBtn = $this->getBtn('/product/picture',$v['id'],'primary','管理图片');
            #管理附件按钮
            $annexBtn = $this->getBtn('/product/annex',$v['id'],'primary','管理附件');
            #编辑产品按钮
            $editBtn = $this->getBtn('/product/edit',$v['id'],'primary','编辑');
            #删除产品按钮
            $deleteBtn = $this->getDeleteBtn($v['id']);
            $button[$k] = [
                [$pictureBtn], [$annexBtn], [$editBtn,$btn_status,$deleteBtn]
            ];
        }
        return parent::dataList($table,$field,$field2,$button,$info);
    }
}
